﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Mvc;
using CubicFWNet.Mvc.Models;
using CubicFWNet.Application.SystemManager;
using CubicFWNet.Common;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.ViewModal;
using PagedList;

namespace CubicFWNet.Mvc.Controllers
{
    public class SalerDirectorController : ControllerBase
    {
        OrderApp _orderApp;
        CustomerApp _customerApp;
        ProductApp _productApp;
        OrderProductApp _orderProductApp;
        InvoiceApp _invoiceApp;
        UserApp _userApp;
        private Guid salersID = new Guid("4cf6a365-e372-42ce-bd05-ceec18b294a4");
        public SalerDirectorController()
        {
            _orderApp = new OrderApp();
            _customerApp = new CustomerApp();
            _productApp = new ProductApp();
            _orderProductApp = new OrderProductApp();
            _invoiceApp = new InvoiceApp();
            _userApp = new UserApp();
        }

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 产品管理
        /// </summary>
        /// <returns></returns>
        public ActionResult ProList(int page = 1, int categoryid = 0, string productName = "")
        {
            ViewBag.ProductName = productName;
            //ViewBag.Type = type;

            IPagedList<ProductView> productList = _productApp.GetList(categoryid, productName).ToPagedList(page, 10);
            return View(productList);
        }

        /// <summary>
        /// 已禁用的产品列表
        /// </summary>
        /// <returns></returns>
        public ActionResult ProDisableList(int page = 1, int categoryid = 0, string productName = "")
        {
            ViewBag.ProductName = productName;

            IPagedList<ProductView> productList = _productApp.GetList(categoryid, productName).ToPagedList(page, 10);
            return View(productList);
        }

        /// <summary>
        /// 产品审核列表
        /// </summary>
        /// <returns></returns>
        public ActionResult ProCheckList(int page = 1, int categoryid = 0, string productName = "")
        {
            ViewBag.ProductName = productName;
            //ViewBag.ProductNum = productNum;
            //ViewBag.Type = type;

            IPagedList<ProductView> productList = _productApp.GetList(categoryid, productName).ToPagedList(page, 10);
            return View(productList);
        }

        /// <summary>
        /// 产品审核
        /// </summary>
        /// <returns></returns>
        public ActionResult ProductCheck()
        {
            string pid = Request.QueryString["PID"];
            Guid id = new Guid(pid);
            Product pro = _productApp.GetInfo(id);
            if (pro != null)
            {
                ViewBag.Product = pro;
            }
            ViewBag.RealName = pro.CreatorName;
            return View();
        }

        /// <summary>
        /// 产品价格审核[提交数据]
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ProductCheck(FormCollection fc)
        {
            string pid = fc["ProductID"];
            Guid id = new Guid(pid);
            Product pro = _productApp.GetInfo(id);
            if (null != pro)
            {
                pro.Price = Convert.ToDecimal(fc["Price"]);
                pro.Status = 1;

                int i = _productApp.Update(pro);
                if (i == 1)
                {
                    return RedirectToAction("ProCheckList", "SalerDirector");
                }
                else
                {
                    ViewBag.Info = "抱歉，提交失败！";
                }
            }

            return View();
        }

        /// <summary>
        /// 更新产品状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult ProChange(string id)
        {
            Guid pid = new Guid(id);
            Product pro = _productApp.GetInfo(pid);
            if (pro.Status == -2)
            {
                pro.Status = 1;
            }
            else if (pro.Status == 1)
            {
                pro.Status = -2;
            }
            int res = _productApp.Update(pro);
            return Json(res);
        }

        /// <summary>
        /// 产品编辑
        /// </summary>
        /// <returns></returns>
        public ActionResult ProEdit()
        {
            string pid = Request.QueryString["PID"];
            Guid id = new Guid(pid);
            Product pro = _productApp.GetInfo(id);
            if (pro != null)
            {
                ViewBag.Product = pro;
            }
            ViewBag.RealName = pro.CreatorName;
            return View();
        }

        /// <summary>
        /// 产品编辑[提交表单]
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ProEdit(FormCollection fc)
        {
            string pid = fc["ProductID"];
            Guid id = new Guid(pid);
            Product pro = _productApp.GetInfo(id);
            if (null != pro)
            {
                pro.Name = fc["ProductName"];
                pro.Number = fc["Number"];
                pro.Format = fc["Format"];
                pro.Memo = fc["Memo"];
                pro.Unit = fc["Unit"];
                //pro.Data = fc["Data"];
                //pro.Explain = fc["Explain"];
                //pro.Pack = fc["Pack"];
                //pro.WorkType = Convert.ToInt32(fc["WorkType"]);
                //pro.PackRequire = Convert.ToInt32(fc["PackRequire"]);
                //pro.PackAmount = Convert.ToInt32(fc["PackAmount"]);

                //pro.Color = fc["Color"];
                //pro.Angel = fc["Angel"];
                //pro.Tolerance = Convert.ToDecimal(fc["Tolerance"]);
                //pro.ToleranceMinus = Convert.ToDecimal(fc["ToleranceMinus"]);
                //pro.Space = Convert.ToDecimal(fc["Space"]);
                //pro.Margin = Convert.ToDecimal(fc["Margin"]);
                //pro.Distance = Convert.ToDecimal(fc["Distance"]);

                pro.Price = Convert.ToDecimal(fc["Price"]);
                pro.SizeWidth = fc["Width"];
                pro.SizeLenth = fc["Length"];
                // pro.Status = Convert.ToInt32(fc["Status"]);

                int i = _productApp.Update(pro);
                if (i == 1)
                {
                    return RedirectToAction("ProList", "SalerDirector");
                }
                else
                {
                    ViewBag.Info = "抱歉，提交失败！";
                }
            }
            ViewBag.RealName = pro.CreatorName;
            return View();
        }

        /// <summary>
        /// 查询客户
        /// </summary>
        /// <returns></returns>
        public ActionResult CusList(int page = 1, string name = "")
        {
            ViewBag.Name = name;
            ViewBag.SalerList = _customerApp.GetUserList(salersID);
            IPagedList<Customer> cusList = _customerApp.GetList(name, new Guid(), 1).ToPagedList(page, 10);
            return View(cusList);
        }

        /// <summary>
        /// 编辑客户
        /// </summary>
        /// <returns></returns>
        public ActionResult EditCus()
        {
            ViewBag.SalerList = _customerApp.GetUserList(salersID);
            ViewBag.RealName = operatorModel.FamilyName;
            string cid = Request.QueryString["CID"];
            Guid id = new Guid(cid);
            Customer cus = _customerApp.GetInfo(id);
            if (cus != null)
            {
                ViewBag.Customer = cus;
            }
            return View();
        }

        /// <summary>
        /// 编辑客户[提交数据]
        /// </summary>
        /// <param name="fc"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditCus(FormCollection fc)
        {
            string cid = fc["CustomerID"];
            Guid id = new Guid(cid);
            Customer cus = _customerApp.GetInfo(id);
            if (null != cus)
            {
                cus.UserId = new Guid(fc["Saler"]);
                cus.UserName = fc["SalerName"];
                cus.Status = Convert.ToInt32(fc["Status"]);

                int i = _customerApp.Update(cus);
                if (i == 1)
                {
                    return RedirectToAction("CusList", "SalerDirector");
                }
                else
                {
                    ViewBag.Info = "抱歉，操作失败！";
                }
            }

            return View();
        }

        /// <summary>
        /// 查询产品申请
        /// </summary>
        /// <returns></returns>
        public ActionResult ApplyList(int page = 1, string start = "", string end = "", string name = "", string number = "")
        {
            DateTime _start = Convert.ToDateTime("2017-1-1");
            DateTime _end = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
            if (start != "")
            {
                _start = Convert.ToDateTime(start);
            }
            if (end != "")
            {
                _end = Convert.ToDateTime(end);
            }

            ViewBag.Start = _start.ToString("yyyy-MM-dd");
            ViewBag.End = _end.ToString("yyyy-MM-dd");
            ViewBag.Name = name;
            ViewBag.Number = number;

            ViewBag.SalerList = _customerApp.GetUserList(salersID);
            IPagedList<OrderInfo> orderList = _orderApp.GetList(_start, _end.AddDays(1), 2, 0, name, "", number).ToPagedList(page, 10);
            return View(orderList);
        }

        /// <summary>
        /// 确认指导价
        /// </summary>
        /// <returns></returns>
        public ActionResult GuidePrice(int page = 1, string start = "", string end = "", string name = "", string number = "")
        {
            DateTime _start = Convert.ToDateTime("2017-1-1");
            DateTime _end = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
            if (start != "")
            {
                _start = Convert.ToDateTime(start);
            }
            if (end != "")
            {
                _end = Convert.ToDateTime(end);
            }

            ViewBag.Start = _start.ToString("yyyy-MM-dd");
            ViewBag.End = _end.ToString("yyyy-MM-dd");
            ViewBag.Name = name;
            ViewBag.Number = number;

            ViewBag.SalerList = _customerApp.GetUserList(salersID);
            IPagedList<OrderInfo> orderList = _orderApp.GetList(_start, _end.AddDays(1), 2, 5, name, "", number).ToPagedList(page, 10);
            return View(orderList);
        }

        /// <summary>
        /// 审核最终价列表
        /// </summary>
        /// <returns></returns>
        public ActionResult CheckPriceList(int page = 1, string start = "", string end = "", string name = "", string number = "")
        {
            DateTime _start = Convert.ToDateTime("2017-1-1");
            DateTime _end = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
            if (start != "")
            {
                _start = Convert.ToDateTime(start);
            }
            if (end != "")
            {
                _end = Convert.ToDateTime(end);
            }

            ViewBag.Start = _start.ToString("yyyy-MM-dd");
            ViewBag.End = _end.ToString("yyyy-MM-dd");
            ViewBag.Name = name;
            ViewBag.Number = number;

            ViewBag.SalerList = _customerApp.GetUserList(salersID);
            IPagedList<OrderInfo> orderList = _orderApp.GetList(_start, _end.AddDays(1), 2, 502, name, "", number).ToPagedList(page, 10);
            return View(orderList);
        }

        /// <summary>
        /// 查询订单列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="name"></param>
        /// <param name="number"></param>
        /// <returns></returns>
        public ActionResult Order(int page = 1, string start = "", string end = "", string name = "", string number = "")
        {
            DateTime _start = Convert.ToDateTime("2017-1-1");
            DateTime _end = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
            if (start != "")
            {
                _start = Convert.ToDateTime(start);
            }
            if (end != "")
            {
                _end = Convert.ToDateTime(end);
            }

            ViewBag.Start = _start.ToString("yyyy-MM-dd");
            ViewBag.End = _end.ToString("yyyy-MM-dd");
            ViewBag.Name = name;
            ViewBag.Number = number;

            ViewBag.SalerList = _customerApp.GetUserList(salersID);
            IPagedList<OrderInfo> orderList = _orderApp.GetList(_start, _end.AddDays(1), 3, 0, name, "", number).ToPagedList(page, 10);
            return View(orderList);
        }

        /// <summary>
        /// 利润核算
        /// </summary>
        /// <returns></returns>
        public ActionResult Check()
        {
            string str = Request.QueryString["ID"];
            OrderInfo order = new OrderInfo();
            List<ProductView> opList = new List<ProductView>();
            if (str != null)
            {
                Guid oID = new Guid(str);
                order = _orderApp.GetFullInfo(oID);
                if (order != null)
                {
                    opList = _orderProductApp.GetListByOrderID(oID).ToList();
                }
            }

            ViewBag.Order = order;
            ViewBag.OpList = opList;
            return View();
        }

        /// <summary>
        /// 提交利润核算
        /// </summary>
        /// <param name="fc"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Check(FormCollection fc)
        {
            Guid oID = new Guid(fc["OrderID"]);
            Order order = _orderApp.GetInfo(oID);

            string proList = fc["Data"];
            JavaScriptSerializer js = new JavaScriptSerializer();
            List<ModelProPrice> ls = js.Deserialize<List<ModelProPrice>>(proList);

            for (int i = 0; i < ls.Count; i++)
            {
                OrderProduct op = _orderProductApp.GetSingleOrderProduct(ls[i].ID);
                op.Price = ls[i].Price;
                _orderProductApp.Update(op);
            }

            order.Status = 501; //下一步，销售填写价格申请
            _orderApp.Update(order);
            return RedirectToAction("GuidePrice", "SalerDirector");

        }

        /// <summary>
        /// 价格审核
        /// </summary>
        /// <returns></returns>
        public ActionResult CheckPrice()
        {
            string str = Request.QueryString["ID"];
            OrderInfo order = new OrderInfo();
            List<ProductView> opList = new List<ProductView>();
            if (str != null)
            {
                Guid oID = new Guid(str);
                order = _orderApp.GetFullInfo(oID);
                if (order != null)
                {
                    opList = _orderProductApp.GetListByOrderID(oID).ToList();
                }
            }

            ViewBag.Order = order;
            ViewBag.OpList = opList;
            return View();
        }

        /// <summary>
        /// 通过价格审核
        /// </summary>
        /// <param name="fc"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult CheckPrice(FormCollection fc)
        {
            Guid oID = new Guid(fc["OrderID"]);
            Order order = _orderApp.GetInfo(oID);

            string proList = fc["Data"];
            JavaScriptSerializer js = new JavaScriptSerializer();
            List<ModelProPrice> ls = js.Deserialize<List<ModelProPrice>>(proList);

            for (int i = 0; i < ls.Count; i++)
            {
                OrderProduct op = _orderProductApp.GetSingleOrderProduct(ls[i].ID);
                op.FinalPrice = ls[i].Price;
                _orderProductApp.Update(op);
            }

            order.Status = 6; //下一步，确认下单
            _orderApp.Update(order);
            return RedirectToAction("CheckPriceList", "SalerDirector");

        }

        /// <summary>
        /// 查询已取消订单列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="name"></param>
        /// <param name="number"></param>
        /// <returns></returns>
        public ActionResult CancelList(int page = 1, string start = "", string end = "", string name = "", string number = "")
        {
            DateTime _start = Convert.ToDateTime("2017-1-1");
            DateTime _end = DateTime.Now.AddDays(1);
            if (start != "")
            {
                _start = Convert.ToDateTime(start);
            }
            if (end != "")
            {
                _end = Convert.ToDateTime(end);
            }

            ViewBag.Start = _start.ToString("yyyy-MM-dd");
            ViewBag.End = _end.ToString("yyyy-MM-dd");
            ViewBag.Name = name;
            ViewBag.Number = number;

            ViewBag.SalerList = _customerApp.GetUserList(salersID);
            IPagedList<OrderInfo> orderList = _orderApp.GetList(_start, _end, -10, 0, name, "", number).ToPagedList(page, 10);
            return View(orderList);
        }

        /// <summary>
        /// 查询已完成订单列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="name"></param>
        /// <param name="number"></param>
        /// <returns></returns>
        public ActionResult FinishList(int page = 1, string start = "", string end = "", string name = "", string number = "")
        {
            DateTime _start = Convert.ToDateTime("2017-1-1");
            DateTime _end = DateTime.Now.AddDays(1);
            if (start != "")
            {
                _start = Convert.ToDateTime(start);
            }
            if (end != "")
            {
                _end = Convert.ToDateTime(end);
            }

            ViewBag.Start = _start.ToString("yyyy-MM-dd");
            ViewBag.End = _end.ToString("yyyy-MM-dd");
            ViewBag.Name = name;
            ViewBag.Number = number;

            ViewBag.SalerList = _customerApp.GetUserList(salersID);
            IPagedList<OrderInfo> orderList = _orderApp.GetList(_start, _end, 10, 0, name, "", number).ToPagedList(page, 10);
            return View(orderList);
        }

        /// <summary>
        /// 订单对应的发货单列表
        /// </summary>
        /// <returns></returns>
        public ActionResult InvoiceList()
        {
            Guid orderId = new Guid(Request.QueryString["OID"]);
            OrderInfo order = _orderApp.GetFullInfo(orderId);
            List<Invoice> invList = _invoiceApp.GetInvoiceListByOrderId(orderId).ToList();
            ViewBag.Order = order;
            return View(invList);
        }

        /// <summary>
        /// 更新客户对应的销售员
        /// </summary>
        /// <param name="cid"></param>
        /// <param name="uid"></param>
        /// <returns></returns>
        public JsonResult UpdateSaler(Guid cid, Guid uid)
        {
            Customer cus = _customerApp.GetInfo(cid);
            User user = _userApp.GetForm(uid);
            cus.UserId = uid;
            cus.UserName = user.RealName;
            _customerApp.Update(cus);
            return Json(1);
        }
    }
}